package com.simba.server.commands.impl.fetch;

import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;
import com.simba.server.components.data.SortInfo;

import java.sql.ResultSet;

public class FetchTestSortInfoCmd implements ICommand {
    public static final String NAME = "FetchTestSortInfoCmd";
    private final SortInfo sortInfo;
    private final String sql = "select * from tb_sort_info_nb where barcode=?;";

    public FetchTestSortInfoCmd(SortInfo sortInfo) {
        this.sortInfo = sortInfo;
    }


    @Override
    public String getName() {
        return FetchTestSortInfoCmd.NAME;
    }

    @Override
    public Boolean run(Object params) {
        Boolean ret = false;

        if (!(params instanceof DbOperator))
        {
            return ret;
        }

        try
        {
            DbOperator dbOperator = (DbOperator) params;

            dbOperator.prepareStatement(this.sql);
            dbOperator.setString(1, sortInfo.getBarcode());

            ResultSet rs = dbOperator.executeQuery();

            while (rs.next())
            {
                sortInfo.getSlotsList().clear();
                int slot1 = rs.getInt("slotOne");
                int slot2 = rs.getInt("slotTwo");
                sortInfo.setRoadSlot(slot1+"");
                sortInfo.getSlotsList().add(slot1);
                sortInfo.getSlotsList().add(slot2);
            }

            ret = true;
        }
        catch (Exception ex)
        {
            ret = false;
        }

        return ret;
    }
}
